package cn.com.bluemoon.daps.sync.core.sync.sql.mysql.datatype.number;

import cn.com.bluemoon.daps.sync.core.sync.sql.mysql.datatype.DataType;
import com.google.common.collect.ImmutableSet;

import java.util.Set;

/**
 * @author Jarod.Kong
 */
public class NumberDataType implements DataType {

    /** 有小数位 */
    public static final Set<String> decimalDataTypes = ImmutableSet.of("decimal", "float", "double");
    public static final Set<String> INT_DATA_TYPES = ImmutableSet.of("int", "tinyint", "smallint", "mediumint", "bigint", "bit");
    public static final Set<String> numberDataType = ImmutableSet.<String>builder().addAll(decimalDataTypes).addAll(INT_DATA_TYPES).build();

    /**
     * @return 名称
     */
    @Override
    public Set<String> name() {
        return numberDataType;
    }

    /**
     * assert  numberDataType.normalizeScale("double", null) == 0;
     * assert  numberDataType.normalizeScale("double", 0) == 0;
     * assert  numberDataType.normalizeScale("double", 10) == 10;
     * assert  numberDataType.normalizeScale("int", null) == null;
     * assert  numberDataType.normalizeScale("int", 0) == null;
     * assert  numberDataType.normalizeScale("int", 10) == 10;
     */
    @Override
    public Integer normalizeScale(String dataType, Integer decimalLength) {
        if (dataType != null) {
            if (numberDataType.contains(dataType.toLowerCase())) {
                if (decimalDataTypes.contains(dataType.toLowerCase())) {
                    if (decimalLength == null) {
                        decimalLength = 0;
                    }
                } else {
                    if (decimalLength == null || decimalLength == 0) {
                        decimalLength = null;
                    }
                }
            }
        }
        return decimalLength;
    }
}
